package model

type SearchEnginesCategory struct {
	ID uint `gorm:"primary_key;AUTO_INCREMENT" json:"id" form:"id"`

	Name string `gorm:"type:text" form:"name" binding:"required" json:"name"`

	SearchEngines []SearchEngines `gorm:"foreignKey:CategoryID;" json:"search_engines"`
}

// Add 添加
func (m *SearchEnginesCategory) Add() (id uint, err error) {
	err = db.Create(&m).Error
	if err == nil {
		id = m.ID
	}
	return
}

// Update 更新
func (m *SearchEnginesCategory) Update() (err error) {
	err = db.Omit("created_at").Model(SearchEnginesCategory{}).Where("id=?", m.ID).Updates(m).Error
	return
}

func DeleteSearchEnginesCategory(id uint) (err error) {
	err = db.Where("id=?", id).Delete(&SearchEnginesCategory{}).Error
	if err == nil {
		db.Where("category_id=?", id).Delete(&SearchEngines{})
	}
	return
}

func GetSearchEnginesCategory() (items []SearchEnginesCategory, err error) {
	err = db.Preload("SearchEngines").Find(&items).Error
	return
}
